package com.budgetme.activities.elements;

import com.budgetme.R;
import com.budgetme.database.Database;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Canvas;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


public class IncomeBar extends LinearLayout {
	
	Context mContext;
	
	public IncomeBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        
        mContext = context;
        
        View v = LayoutInflater.from(mContext).inflate(R.layout.income_bar, this, true);
        
        loadContent(v);

	}
	
	public void loadContent(View v) {
		
		TextView incomeView = (TextView) v.findViewById(R.id.income_bar_income_value);
		TextView budgetView = (TextView) v.findViewById(R.id.income_bar_budget_value);
		
		// Get Income Total
		Uri uri = Uri.withAppendedPath(Database.tbl_income.CONTENT_URI, "sum");
		Cursor cursor = mContext.getContentResolver().query(uri, null, null, null, null);
		
		cursor.moveToFirst();
		double income = cursor.getDouble(cursor.getColumnIndex(Database.tbl_income.i_amount));
		cursor.close();
		
		
		// Get budget Total
		uri = Uri.withAppendedPath(Database.tbl_budgets.CONTENT_URI, "sum");
		cursor = mContext.getContentResolver().query(uri, null, null, null, null);
		
		cursor.moveToFirst();
		double budgeted = cursor.getDouble(cursor.getColumnIndex(Database.tbl_budgets.t_budget));
		cursor.close();
		
		double spent = 0;
		
		
		if (income > budgeted) {
			incomeView.setTextColor(getResources().getColor(android.R.color.holo_green_light));
		} else {
			incomeView.setTextColor(getResources().getColor(android.R.color.holo_red_light));
		}
		
		if (budgeted > spent) {
			budgetView.setTextColor(getResources().getColor(android.R.color.holo_green_light));
		} else {
			budgetView.setTextColor(getResources().getColor(android.R.color.holo_red_light));
		}
		
		incomeView.setText(String.valueOf(income));
		budgetView.setText(String.valueOf(budgeted));
		
		invalidate();
		
	}
	
}
